home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / TODO.PAK / TODOWIN.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  4KB  |  150 lines

  1. //---------------------------------------------------------------------
  2. //
  3. //  TODOWIN.CPP - part of TODO example program
  4. //
  5. //      Copyright (c) 1991, 1993 by Borland International
  6. //      All Rights Reserved.
  7. //
  8. //---------------------------------------------------------------------
  9.  
  10. #define STRICT
  11. #define NOMINMAX
  12.  
  13. #include <services/wsysinc.h>
  14. #include <services/except.h>
  15. #include <services/checks.h>
  16.  
  17. #include "todowin.h"
  18.  
  19. HINSTANCE WinBase::hInst;
  20. HINSTANCE WinBase::hPrevInst;
  21. LPSTR     WinBase::Cmd;
  22. int       WinBase::Show;
  23.  
  24. //---------------------------------------------------------------------
  25. //
  26. //  members and data for class ModalDialog
  27. //
  28. //---------------------------------------------------------------------
  29.  
  30. ModalDialog *ModalDialog::CurDlg = 0;
  31.  
  32. UINT ModalDialog::Run()
  33. {
  34.     FARPROC dlgProc =
  35.         MakeProcInstance( (FARPROC)ModalDialog::DlgProc, (HINSTANCE)hInst );
  36.     DialogBox( (HINSTANCE)hInst, 
  37.                (LPCSTR)GetDialogName(), 
  38.                (HWND)hWnd(), 
  39.                (DLGPROC)dlgProc );
  40.     FreeProcInstance( dlgProc );
  41.     return Result;
  42. }
  43.  
  44. BOOL CALLBACK _export ModalDialog::DlgProc( HWND hDlg,
  45.                 UINT msg,
  46.                 WPARAM wParam,
  47.                 LPARAM lParam
  48.                                             )
  49. {
  50.     return CurDlg->Dispatch( hDlg, msg, wParam, lParam );
  51. }
  52.  
  53. BOOL ModalDialog::Dispatch( HWND, UINT, WPARAM, LPARAM )
  54. {
  55.     return FALSE;
  56. }
  57.  
  58. //---------------------------------------------------------------------
  59. //
  60. //  members and data for class Window
  61. //
  62. //---------------------------------------------------------------------
  63.  
  64. Window *Window::InCreate = 0;
  65. Window *Window::WinList = 0;
  66.  
  67. BOOL Window::Create()
  68. {
  69.     if( hPrevInst == 0 && RegisterClass() == FALSE )
  70.         {
  71.         return FALSE;
  72.         }
  73.  
  74.     InCreate = this;            // flag that we're inside CreateNewWindow()
  75.  
  76.     CreateNewWindow();
  77.  
  78.     NextWin = WinList;          // insert this object into the Window list
  79.     WinList = this;
  80.  
  81.     InCreate = 0;               // now it's OK to use normal dispatching
  82.  
  83.     return TRUE;
  84. }
  85.  
  86. UINT Window::Run()
  87. {
  88.     PRECONDITION( hWnd() != 0 );// check that we really exist
  89.  
  90.     MSG msg;
  91.     while( GetMessage( &msg, NULL, NULL, NULL ) != 0 )
  92.         {
  93.         TranslateMessage( &msg );
  94.         DispatchMessage( &msg );
  95.         }
  96.     return msg.wParam;
  97. }
  98.  
  99. LONG Window::Dispatch( UINT msg, WPARAM wParam, LPARAM lParam )
  100. {
  101.     return DefWindowProc( (HWND)hWnd(), msg, wParam, lParam );
  102. }
  103.  
  104. LRESULT CALLBACK Window::WndProc( HWND hWnd,
  105.                                   UINT msg,
  106.                                   WPARAM wParam,
  107.                                   LPARAM lParam )
  108. {
  109.     try {
  110.         Window *cur = Window::WinList;
  111.  
  112.         //  look up the handle in our Window list
  113.         while( cur != 0 && cur->hWnd() != hWnd )
  114.             cur = cur->NextWin;
  115.  
  116.         //  normal dispatching
  117.         if( cur != 0 )
  118.             return cur->Dispatch( msg, wParam, lParam );
  119.  
  120.         //  if we're inside CreateNewWindow(), assume
  121.         //  that the message is for us
  122.         if( InCreate != 0 )
  123.             {
  124.             InCreate->hWindow = hWnd;
  125.             return InCreate->Dispatch( msg, wParam, lParam );
  126.             }
  127.  
  128.         //  otherwise, pass it on to windows
  129.         return DefWindowProc( hWnd, msg, wParam, lParam );
  130.         }
  131.     catch( const xmsg& msg )
  132.         {
  133.         MessageBox( InCreate ? InCreate->hWindow : hWnd,
  134.                     msg.why().c_str(),
  135.                     "Fatal Error",
  136.                     MB_ICONSTOP | MB_OK );
  137.         PostQuitMessage(0);
  138.         }
  139.     catch( ... )
  140.         {
  141.         MessageBox( InCreate ? InCreate->hWindow : hWnd,
  142.                     "Unknown error",
  143.                     "Fatal Error",
  144.                     MB_ICONSTOP | MB_OK );
  145.         PostQuitMessage(0);
  146.         }
  147.     return DefWindowProc( hWnd, msg, wParam, lParam );
  148. }
  149.  
  150.